package cn.xopencode.userservice.manager;

import cn.hutool.core.collection.CollUtil;
import cn.xopencode.common.framework.vo.PageResult;
import cn.xopencode.userservice.rpc.dept.dto.*;
import cn.xopencode.userservice.service.DeptService;
import cn.xopencode.userservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author xiaodawei
 * @since 2021/9/17
 */
@Component
public class DeptManager {

    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;


    public DeptDTO getDept(String deptId) {
        return deptService.getDept(deptId);
    }


    public String CreateDept(CreateDeptDTO createDeptDTO) {
        return deptService.CreateDept(createDeptDTO);
    }

    public PageResult<DeptDTO> findDeptList(DeptPageDTO deptPageDTO) {
        return deptService.findDeptList(deptPageDTO);
    }

    public Integer updateDept(List<UpdateDeptDTO> updateDeptDTOList) {
        return deptService.updateDept(updateDeptDTOList);
    }

    public Integer updateProperty(List<UpdatePropertyDTO> updatePropertyDTOList) {
        return deptService.updateProperty(updatePropertyDTOList);
    }


    public List<DeptDTO> getDeptList(String parentId) {
        return deptService.getDeptList(parentId);
    }

    public String getDeptIdList(String deptId) {
        String deptIds = null;
        List<DeptDTO> deptList = getDeptList(deptId);
        if (CollUtil.isNotEmpty(deptList)) {
            List<String> list = deptList.stream().map(DeptDTO::getId).collect(Collectors.toList());
            deptIds = String.join(",", list);
        }
        return deptIds;
    }

    public String getDeptNameList(String deptIds) {
        return deptService.getDeptNameList(deptIds);
    }

    public DeptTypeDTO getDeptTypeDTO(String userId) {
        return deptService.getDeptTypeName(userService.getUser(userId).getDeptId());
    }
}
